using System;

namespace AntEater.Runtime.UI
{
    /// <summary>
    /// Base class for each presenter
    /// </summary>
    public abstract class PresenterBase : IPresenter, IDisposable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PresenterBase"/> class.
        /// </summary>
        protected PresenterBase() {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PresenterBase"/> class.
        /// </summary>
        /// <param name="view">The view.</param>
        protected PresenterBase(IView view) {
            this._view = view;
        }

        private PresenterContext _context;

        /// <summary>
        /// Gets the context.
        /// </summary>
        /// <value>The context.</value>
        protected PresenterContext Context {
            get { return _context; }
        }

        private bool _isShutdown;

        /// <summary>
        /// Gets a value indicating whether this instance is shutdown.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is shutdown; otherwise, <c>false</c>.
        /// </value>
        public bool IsShutdown {
            get { return _isShutdown; }
        }

        private IView _view;

        /// <summary>
        /// Gets the view.
        /// </summary>
        /// <value>The view.</value>
        public virtual IView View {
            get { return _view; }
            protected set { _view = value; }
        }      

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        /// <param name="context">The context.</param>
        public virtual void Initialize(PresenterContext context) {
            this._context = context;
        }

        /// <summary>
        /// Refreshes this instance.
        /// </summary>
        public virtual void Refresh() {
            this.View.Refresh();
        }

        /// <summary>
        /// Shutdowns this instance.
        /// </summary>
        public virtual void Shutdown() {
            this._isShutdown = true;
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose() {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Disposes the specified disposing.
        /// </summary>
        /// <param name="disposing">if set to <c>true</c> [disposing].</param>
        protected virtual void Dispose(bool disposing) {

            if (disposing) {
                // Dispose view
                if (_view != null) {
                    _view.Surface.Dispose();
                }
            }
        }
    }
}
